package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfUnit;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ConfUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
/**
 * 单位配置表-Controller
 * @author zfsn
 * @date 2023-6-12
 */
@Api(tags = "单位配置表功能接口")
@RestController
@RequestMapping("/unit")
public class ConfUnitController {

	@Autowired
	private ConfUnitService confUnitService;

	/**
	 * 通过主键查询数据
	 * @param unitCodes 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过单位编码查询数据", notes = "通过单位编码查询数据，支持批量查询")
	@GetMapping("/{codes}")
	public R<List<ConfUnit>> queryByIds(@PathVariable("codes") List<String> unitCodes) {
		LambdaQueryWrapper<ConfUnit> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(ConfUnit::getCode,unitCodes);
		return R.ok(confUnitService.list(queryWrapper));
	}

	/**
	 * 分页查询
	 * @param confUnit 筛选条件
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "所属系统id", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "userName", value = "用户名称(支持模糊查询)", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "单位名称(支持模糊查询)", paramType = "body", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "orderByColumn", value = "排序列", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAsc", value = "排序的方向值只能是 desc 或者 asc", paramType = "body", required = true, dataType = "String")
	})
	public R<Page<ConfUnit>> listPage(@RequestBody @ApiIgnore ConfUnit confUnit) {
		return R.ok(confUnitService.listPage(confUnit));
	}

	/**
	 * 新增数据
	 * @param confUnit 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@PostMapping("/save")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemIds", value = "所属系统id(多选）", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "userIds", value = "用户名称(多选)", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "单位名称", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> save(@RequestBody @ApiIgnore @Validated(InsertFlag.class) ConfUnit confUnit) {
		return confUnitService.saveData(confUnit);
	}

	/**
	 * 通过主键更新数据
	 * @param confUnit 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键更新数据", notes = "通过主键更新数据")
	@PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemIds", value = "所属系统id(多选）", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "userIds", value = "用户名称(多选)", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "单位名称", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "code", value = "单位编码", paramType = "body", required = true, dataType = "String"),
	})
	public R<String> updateData(@RequestBody @ApiIgnore ConfUnit confUnit) {
		return confUnitService.updateData(confUnit);
	}

	/**
	 * 通过主键删除数据
	 * @param codes 主键
	 * @return 通用返回对象
	 */
	@DeleteMapping("/delete/{codes}")
	public R<String> removeById(@PathVariable("codes") @ApiIgnore List<String> codes){
		LambdaUpdateWrapper<ConfUnit> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.in(ConfUnit::getCode, codes);
		confUnitService.remove(updateWrapper);
		return R.ok("删除系统成功");
	}

}